const express = require('express');
const path = require('path');
const cheerio = require('cheerio');
const superagent = require('superagent');

const app = express();
app.use('/node_modules', express.static(path.join(__dirname, '..', 'node_modules')));
app.use(express.static(path.join(__dirname, '..', 'public')));
app.engine('.html', require('ejs').__express);

// Optional since express defaults to CWD/views

app.set('views', path.join(__dirname, '..', 'views'));
// Without this you would need to
// supply the extension to res.render()
// ex: res.render('users.html').
app.set('view engine', 'html');
// 抓取百度新闻
app.get('/baiduNews', async (req, oRes) => {
  // 利用superagent发送请求，获取爬虫所需数据
  try {
    superagent.get('https://news.baidu.com/').end((err, res) => {
      if (err) {
        return console.log('err', err);
      }
      // 成功，则爬虫得到的数据保存在res.text属性中
      const HtmlStr = res.text;
      // 利用cheerio解析html字符串，生成 DOM 结构
      const $ = cheerio.load(HtmlStr);
      const result = [];
      // 利用jQuery语法获取到爬虫内容对应的具体DOM元素
      $('.hotnews ul li a').each((index, Element) => {
        // 遍历保存每一项的信息
        let obj = {
          title: $(Element).text(),
          href: $(Element).attr('href')
        };
        result.push(obj);
      });
      // console.log('result', result);
      oRes.render('baiduNews', {
        data: result,
        title: 'baidu news',
        header: 'baidu news head'
      });
    });
  } catch (err) {
    console.error('Error:', err);
    oRes.status(500).json({ error: 'An error occurred' });
    // oRes.render('baiduNews', {
    //   data: [],
    //   title: 'baidu news',
    //   header: 'baidu news head'
    // });
  }
});
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
